<?php

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');

/**
 * 报名
 */
class apply extends foreground {

    public function __construct() {
        parent::__construct();


    }

    //课程报名
    public function doclass() {
        $info = $this->memberinfo;

        include template('member', 'apply_doclass');
    }
    //我要当导师
    public function doteacher() {
        $info = $this->memberinfo;

        include template('member', 'apply_doteacher');
    }
    public function do_teacher() {
        $db_apply_teacher = pc_base::load_model('apply_teacher_model');

        $uid = (int)param::get_cookie('_userid');
        if (!empty($_POST['realname']) && isset($_POST['realname']) && is_badword($_POST['realname']) == false) {
            $info['realname'] = $_POST['realname'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if (!empty($_POST['mobile']) && isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {
            $info['mobile'] = $_POST['mobile'];
        } else {
            echo '请提供正确的手机号码！';exit();
        }
        if (!empty($_POST['trade']) && isset($_POST['trade']) && is_badword($_POST['trade']) == false) {
            $info['trade'] = $_POST['trade'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if (!empty($_POST['job']) && isset($_POST['job']) && is_badword($_POST['job']) == false) {
            $info['job'] = $_POST['job'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if (!empty($_POST['goodjob']) && isset($_POST['goodjob']) && is_badword($_POST['goodjob']) == false) {
            $info['goodjob'] = $_POST['goodjob'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if ($_POST['type']!=',' && isset($_POST['type'])) {
            $info['type'] =substr(str_replace(' ', '', $_POST['type']),0,-1) ;
        } else {
            echo '输入信息不合法！';exit();
        }
        $info['uid']=$uid;
        $info['ip']=ip();
        $info['ymd']=date('Y-m-d');
        $info['ymdhis']=date('Y-m-d H:i:s');        
        $info['status']=0;
        
        $res = $db_apply_teacher->insert($info);
        if (!$res) {
            echo '申请失败';exit();
        }
        echo '您已经成功提交了申请，之后会有工作人员给您电话沟通，请保持电话畅通';exit();

    }
    
    //我要约见
    public function doyuejian() {
        $info = $this->memberinfo;

        include template('member', 'apply_doyuejian');
    }

    //ajax
    public function doclass_ajax() {
        $uid = (int)param::get_cookie('_userid');
        $a_catid = $_POST['a_catid'] ? (int)$_POST['a_catid'] : 0;
        $a_id = $_POST['a_id'] ? (int)$_POST['a_id'] : 0;

        $info = $_POST;

        foreach ($info as $k => &$v) {
            $v = safe_replace($v);
            if (in_array($k, ['realname', 'job', 'mobile', 'company']) && !$v) {
                json_exit(1, 200, '带 * 为必填项');
            }
        }

        if (!preg_match("/^1\d{10}$/", $info['mobile'])) {
            json_exit(1, 200, '请提供正确的手机号码');
        }

        $db_apply_class = pc_base::load_model('apply_class_model');

        $is_hasbuy = $db_apply_class->count(['uid' => $uid, 'a_catid' => $a_catid, 'a_id' => $a_id]);
        if ($is_hasbuy) {
            json_exit(1, 300, '您已经报名过了,无序重复报名');
        }

        //扣分
        $db_member = pc_base::load_model('member_model');
        $cha = $db_member::log($uid, -50, $db_member::TYPE_APPLY, "报名-$a_catid-$a_id", __METHOD__);
        if ($cha) {
            json_exit('null', 300, "报名需要50积分, 还需$cha");
        }

        $db_content = pc_base::load_model('content_model');
        $db_content->table_name = 'v9_classmany';
        $sql = "update {$db_content->table_name} set student_has=student_has+1 where id=$a_id";
        $db_content->query($sql);

        //记录
        $info = array_merge($info, [
            'uid' => $uid,
            'a_catid' => $a_catid,
            'a_id' => $a_id,
            'ip' => ip(),
            'ymd' => date('Y-m-d'),
            'ymdhis' => date('Y-m-d H:i:s'),
            'status' => $db_apply_class::TYPE_NONE,
        ]);
        $res = $db_apply_class->insert($info);

        if (!$res) {
            json_exit(1, 300, '报名失败');
        }

        json_exit(1, 100, '报名成功');
    }
   
    //报名成功
    public function doclass_success() {
        include template('member', 'apply_doclass_success');
    }
    //预约名师
    public function add_yuyue() {
        $db_apply_expert = pc_base::load_model('apply_expert_model');
        $info['uid'] = (int)param::get_cookie('_userid');        
        $info['a_id'] = $_POST['a_id'] ? (int)$_POST['a_id'] : 0;   
        $info['jieguo_date'] = $_POST['date'] ? (int)$_POST['date'] : 0; 
        $info['num'] = $_POST['num'] ? (int)$_POST['num'] : 0;      

        if (!empty($_POST['realname']) && isset($_POST['realname']) && is_badword($_POST['realname']) == false) {
            $info['realname'] = $_POST['realname'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if (!empty($_POST['mobile']) && isset($_POST['mobile']) && preg_match("/^1\d{10}$/", $_POST['mobile'])) {
            $info['mobile'] = $_POST['mobile'];
        } else {
            echo '请提供正确的手机号码！';exit();
        }
        
        if (isset($_POST['title1']) && is_badword($_POST['title1']) == false) {
            $info['title1'] = $_POST['title1'];
        } else {
            echo '输入信息不合法！';exit();
        }
        if (isset($_POST['title2']) && is_badword($_POST['title2']) == false) {
            $info['title2'] = $_POST['title2'];
        } else {
            echo '输入信息不合法！';exit();
        }
        $info['ip']=ip();
        if ($_POST['con']!='描述一下您的需求背景'&&isset($_POST['con']) && is_badword($_POST['con']) == false) {
            $info['con'] = $_POST['con'];
        } else {
            echo '请输入正确信息！';exit();
        }
        $info['ymd']=date('Y-m-d');
        $info['ymdhis']=date('Y-m-d H:i:s');
        $info['type'] = $_POST['type'] ? (int)$_POST['type'] : 0;
        $info['status']=0;

        $res = $db_apply_expert->insert($info);
        if (!$res) {
            echo '预约失败';exit();
        }
        echo '预约申请已成功';exit();
        
    }
    

}